package com.huarui.company.service;

import java.util.HashMap;
import java.util.Map;

import javax.mail.internet.AddressException;
import javax.mail.internet.ParseException;

import org.ewaf.framework.service.BaseService;

import com.huarui.company.dao.ICompanyDao;
import com.huarui.company.dao.IRegisterInvitationDao;
import com.huarui.company.model.Company;
import com.huarui.employee.model.AddingEmployee;
import com.huarui.user.dao.IUserDao;
import com.huarui.user.service.SendEmail;

public class InvitationService extends BaseService {
	/**
	 * 得到registerDao
	 * 
	 * @return
	 */
	private IUserDao userDao;

	private IRegisterInvitationDao getDao() {
		return (IRegisterInvitationDao) dao;
	}
    private ICompanyDao companyDao;
	private SendEmail sendEmail;
	private String registerInvitationEmailSubjectTemplate;
	private String registerInvitationEmailContentTemplate;
	private String empInvitationEmailSubjectTemplate;
	private String empInvitationEmailContentTemplate;
    
	
	public ICompanyDao getCompanyDao() {
		return companyDao;
	}

	public void setCompanyDao(ICompanyDao companyDao) {
		this.companyDao = companyDao;
	}

	public IUserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public SendEmail getSendEmail() {
		return sendEmail;
	}

	public void setSendEmail(SendEmail sendEmail) {
		this.sendEmail = sendEmail;
	}

	public String getRegisterInvitationEmailSubjectTemplate() {
		return registerInvitationEmailSubjectTemplate;
	}

	public void setRegisterInvitationEmailSubjectTemplate(
			String registerInvitationEmailSubjectTemplate) {
		this.registerInvitationEmailSubjectTemplate = registerInvitationEmailSubjectTemplate;
	}

	public String getRegisterInvitationEmailContentTemplate() {
		return registerInvitationEmailContentTemplate;
	}

	public void setRegisterInvitationEmailContentTemplate(
			String registerInvitationEmailContentTemplate) {
		this.registerInvitationEmailContentTemplate = registerInvitationEmailContentTemplate;
	}

	public void setEmpInvitationEmailSubjectTemplate(
			String empInvitationEmailSubjectTemplate) {
		this.empInvitationEmailSubjectTemplate = empInvitationEmailSubjectTemplate;
	}

	public String getEmpInvitationEmailContentTemplate() {
		return empInvitationEmailContentTemplate;
	}

	public void setEmpInvitationEmailContentTemplate(
			String empInvitationEmailContentTemplate) {
		this.empInvitationEmailContentTemplate = empInvitationEmailContentTemplate;
	}

	public String getEmpInvitationEmailSubjectTemplate() {
		return empInvitationEmailSubjectTemplate;
	}

	/**
	 * 发送邮件,邀请注册公司
	 * 
	 * @throws Exception
	 * @throws ParseException
	 * @throws AddressException
	 */
	public void sendRegisterInvitation(String acceptEmail, String invitorName,
			Integer id) throws Exception {
		Map<String, String> data = new HashMap<String, String>();
		data.put("charset", "UTF-8");
		data.put("invitorName", invitorName);
		data.put("inviteNo", id.toString());
		data.put("subjectTemplate", registerInvitationEmailSubjectTemplate);
		data.put("contentTemplate", registerInvitationEmailContentTemplate);
		data.put("recipients", acceptEmail);
		Company company = companyDao.getCompanyByEmail(acceptEmail);
		if (company != null) {
			
               throw new RuntimeException("公司已经注册");
			
		}
		try{
			
		sendEmail.send(data);
		
		}catch (Exception e) {
			
			  throw new RuntimeException("发送邮件失败");
		}
	}

	public void sendEmpInvitation(AddingEmployee addingEmployee,String password,boolean hasRegister) {
		Map<String, String> data = new HashMap<String, String>();
		data.put("charset", "UTF-8");
		data.put("creatorId", addingEmployee.getCreatorId().toString());
		data.put("creatorName", addingEmployee.getCreatorName());
		data.put("companyName", addingEmployee.getCompanyName());
		data.put("recipients",addingEmployee.getAddeeEmail());
		data.put("subjectTemplate", empInvitationEmailSubjectTemplate);
		data.put("contentTemplate", empInvitationEmailContentTemplate);
		data.put("activateCode", addingEmployee.getActivateCode());
		if (hasRegister) {
			data.put("has_register", "true");
		}else{
			data.put("has_register", "false");
			data.put("password",password);
		}
		sendEmail.send(data);
	}

}
